package com.evil.common.mongo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.evil.common.mongo.entity.AbstractDoubleName;
import com.evil.common.mongo.entity.DoubleName;
import com.evil.common.mongo.repository.DoubleNameRepository;
import com.evil.common.mongo.service.DoubleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Example;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.io.Serializable;
import java.util.List;

/**
 * 统一封装db操作
 *
 * @author liyang
 * @date 2024-05-23 14:57
 */
@Slf4j
public class DoubleServiceImpl<NAME extends Serializable, M extends DoubleNameRepository<T, NAME> & MongoRepository<T, AbstractDoubleName<NAME>>, T extends DoubleName<NAME>>
        extends ServiceImpl<AbstractDoubleName<NAME>, M, T> implements DoubleService<NAME, T> {

    @Override
    public String name2Str(AbstractDoubleName<NAME> name) {
        return name.print();
    }

    @Override
    public List<T> findBySingleNameOrDoubleName(NAME key) {
        return this.repository.findBySingleNameOrDoubleName(key);
    }

    @Override
    public List<T> findListBySingleName(NAME singleName) {
        AbstractDoubleName<NAME> abstractDoubleName = AbstractDoubleName.buildSingle(singleName);
        T t = BeanUtil.copyProperties(abstractDoubleName, this.entityClass);
        return this.repository.findAll(Example.of(t));
    }

    @Override
    public List<T> findListByDoubleName(NAME doubleName) {
        AbstractDoubleName<NAME> abstractDoubleName = AbstractDoubleName.buildDouble(doubleName);
        T t = BeanUtil.copyProperties(abstractDoubleName, this.entityClass);
        return this.repository.findAll(Example.of(t));
    }
}
